#pragma once
#include <functional>
#include "UdpSocket.hpp"

namespace UdpEchoServerModule
{
    using namespace UdpSocketModule;
    using handler = std::function<std::string(std::string)>;
    class UdpEchoServer
    {
    public:
        UdpEchoServer(handler func, std::string ip = "0.0.0.0", uint16_t port = defaultport)
         : _port(port)
         , _func(func)
        {
            _socket.Bind(ip, port);
        }

        void Start()
        {
            while (true)
            {
                std::string msg;
                uint16_t client_port;
                std::string client_ip;
                _socket.Recvfrom(&msg, &client_port, &client_ip);
                msg = _func(msg);
                _socket.Sendto(msg, client_port, client_ip);
            }
        }

    private:
        UdpSocket _socket;
        uint16_t _port;
        handler _func;
    };
}